<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="colseDialog" :close-on-click-modal="false">
    <!-- 表单 -->
    <el-form label-width="120px" :model="formData" :rules="rules" ref="formData">
      <el-form-item label="姓名" prop="username">
        <el-input style="width:50%" placeholder="请输入姓名" v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input style="width:50%" placeholder="请输入手机号" v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker style="width:50%" placeholder="请选择入职时间" v-model="formData.timeOfEntry" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select style="width:50%" placeholder="请选择" v-model="formData.formOfEmployment">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input style="width:50%" placeholder="请输入工号" v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input style="width:50%" placeholder="请选择部门" v-model="formData.departmentName"
          @click.native.stop="getDepartments" />
        <div v-if="showTree" class="tree-box">
          <el-tree :data="treeData" :props="{ label: 'name' }" v-loading="loading" @node-click="selectNode" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker style="width:50%" placeholder="请选择转正时间" v-model="formData.correctionTime" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="colseDialog">取消</el-button>
      <el-button type="primary" @click="submitAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentListAPI } from '@/api/department'
import { addEmployeeAPI } from '@/api/employees'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    }
  },
  data() {
    return {
      // 表单数据
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 表单验证
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 是否显示tree
      loading: false, // 是否加载中
      EmployeeEnum // 聘用形式
    }
  },
  methods: {
    // 提交表单
    submitAdd() {
      this.$refs.formData.validate(async valid => {
        if (valid) {
          await addEmployeeAPI(this.formData)
          this.$message.success('添加成功')
          this.colseDialog()
          this.$emit('updateList')
        }
      })
    },
    // 关闭弹窗
    colseDialog() {
      this.showTree = false
      this.$emit('update:showDialog', false)
      this.$refs.formData.resetFields()
    },
    // 获取部门数据
    async getDepartments() {

      this.showTree = !this.showTree
      // 如果树形控件没有展示, 直接return
      if (!this.showTree) return
      // 获取部门列表数据
      this.loading = true
      const res = await getDepartmentListAPI()
      // console.log('获取部门列表', res.data.depts)
      // depts是数组 但不是树形, 需要转换成属性结构, 才能被 el-tree 展示
      const treeData = tranListToTreeData(res.data.depts, '')
      this.treeData = treeData
      this.loading = false
      // console.log(this.treeData)
    },
    // 点击树节点
    selectNode(nodeData) {
      // console.log(node)
      if (nodeData.children && nodeData.children.length > 0) return
      this.formData.departmentName = nodeData.name
      this.showTree = false
    }
  },
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;

  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>